import http from './http';
import {API_URL_LARAVEL,API_URL_SERVICES} from './env';
import { getQueryObj } from '../utils/url';

const queryObj=getQueryObj();
//获取盲盒活动页
function getBlindboxNum(){
  return http.post('/yfs/api/act/getblindboxactivity',{
    token:localStorage.getItem('token')||queryObj.token
  })
}

//领取盲盒活动奖励
function getBlindboxPrize(type=3){
  const platform=queryObj.platform||'mp-wx';
  let source_pay=platform=='mp-wx'?1:platform=='android'?2:platform=='ios'?3:-1;
  return http.post('/yfs/api/act/getactblindboxprize',{
    token:localStorage.getItem('token')||queryObj.token,
    act_type:type,
    source_pay:source_pay
  })
}

//刷新token
function refreshToken(){
  return http.post(API_URL_SERVICES+'/v1/auth/refresh_token',{
    refresh_token: queryObj.refresh_token,
		user_id: queryObj.user_id
  }).then(res =>{
    if(res.code==0&&res.data.token){
      localStorage.setItem('token',res.data.token);
      return Promise.resolve(res.data.token);
    }else if(res.code==403){
      uni.postMessage({
        data: {
          action: 'login',
          name:'blind_box',
          path:location.origin+location.pathname
        }
      });
      if(queryObj.platform=="mp-wx"){
        uni.navigateBack();
      }
      // uni.redirectTo({
      //   url:"/pagesMine/my_login",
      // });
      return Promise.reject();
    }else{
      return Promise.reject();
    }
  })
}

export {
  getBlindboxNum,getBlindboxPrize,refreshToken
}